package com.liuyk.commonlib.adapter;

import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;

import java.util.List;

/**
 * 基类
 * <p>
 * Created by liuyk on 2018/11/10.
 */
public abstract class BaseRecycleAdapter<H extends RecyclerView.ViewHolder, T> extends RecyclerView.Adapter<H> {
    protected Context mContext;
    protected List<T> mItems;

    protected OnItemClickListener<H> mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener<H> mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public BaseRecycleAdapter(Context context) {
        mContext = context;
    }

    public void setItems(List<T> items) {
        mItems = items;
    }

    protected T getItem(int position) {
        return mItems.get(position);
    }

    public List<T> getItems() {
        return mItems;
    }

    @Override
    public void onBindViewHolder(final H holder, final int position) {
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClick(holder, position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mItems != null ? mItems.size() : 0;
    }

    public abstract class BaseViewHolder extends RecyclerView.ViewHolder {

        public BaseViewHolder(View itemView) {
            super(itemView);
        }
    }

    public interface OnItemClickListener<H> {
        void onItemClick(H holder, int position);
    }
}
